package com.itheima.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;

import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.domain.PageBean;
import com.itheima.utils.JDBCUtils;

/**
 * 数据库表t_book增删查改Service层操作
 * 
 * @author 
 *
 */
public class BookService {

	/**
	 * 查询所有的书籍信息
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Book> findAllBook() throws SQLException {

		return new BookDao().findAllBook();
	}

	/**
	 * 查询某个id的书籍信息
	 * 
	 * @param bid
	 * @return
	 * @throws SQLException
	 */
	public Book findBookById(Integer bid) throws SQLException {

		return new BookDao().findBookById(bid);
	}

	/**
	 * 更新书籍操作
	 * 
	 * @param book
	 * @return
	 * @throws SQLException
	 */
	public int update(Book book) throws SQLException {

		return new BookDao().update(book);
	}

	/**
	 * 添加操作
	 * 
	 * @param book
	 * @return
	 * @throws SQLException
	 */
	public int addBook(Book book) throws SQLException {
		return new BookDao().addBook(book);
	}

	/**
	 * 删除操作
	 * 
	 * @param bid
	 * @return
	 * @throws SQLException
	 */
	public int deleteBook(Integer bid) throws SQLException {
		return new BookDao().deleteBook(bid);
	}

	/**
	 * 批量删除操作
	 * 
	 * @param bid
	 * @return
	 * @throws SQLException
	 */
	public boolean deleteAllBook(String[] bids) throws SQLException {
		Connection conn = null;
		try {
			conn = JDBCUtils.getConnection();
			for (String bid : bids) {
				int i = deleteBook(Integer.parseInt(bid));
				if (i == 0) {
					System.out.println("删除失败！");
					throw new Exception();
				}
			}
			DbUtils.commitAndCloseQuietly(conn);

		} catch (Exception e) {
			DbUtils.rollbackAndCloseQuietly(conn);
			return false;
		}
		return true;
	}

	/**
	 * 根据书籍名称 价格区间查询
	 * 
	 * @param bookName
	 * @param s_price
	 * @param e_price
	 * @throws SQLException
	 */
	public List<Book> search(String bookName, String s_price, String e_price)
			throws SQLException {
		BookDao bookDao = new BookDao();
		return bookDao.search(bookName, s_price, e_price);
	}

	/**
	 * 分页查询
	 * @param currPage
	 * @param bookName
	 * @param s_price
	 * @param e_price
	 * @return
	 * @throws SQLException
	 */
	public PageBean<Book> findBookByPage(Integer currPage,String bookName, String s_price, String e_price) throws SQLException {
		PageBean<Book> pageBean = new PageBean<Book>();
		// 设置pageBean的参数:
		// 设置当前页数:
		pageBean.setCurrPage(currPage);
		// 设置每页显示记录数:
		int pageSize = 10;
		pageBean.setPageSize(pageSize);
		// 设置总记录数:
		BookDao bookDao = new BookDao();
		int totalCount = bookDao.findCount(bookName, s_price, e_price);
		pageBean.setTotalCount(totalCount);
		// 设置总页数:
		int totalPage = 0;
		if (totalCount % pageSize == 0) {
			totalPage = totalCount / pageSize;
		} else {
			totalPage = totalCount / pageSize + 1;
		}		
		pageBean.setTotalPage(totalPage);
		// 设置每页显示的数据的集合:
		int begin = (currPage - 1) * pageSize;
		List<Book> list = bookDao.findByPage(begin, pageSize,bookName, s_price, e_price);
		pageBean.setList(list);
		return pageBean;
	}
}
